/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package delooratomssqlmigra;

/**
 *
 * @author Prk
 */

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;

public class InsThread implements Runnable{
    private Connection ConnMSSQL;
    private PreparedStatement stMSSQL;
    private Connection ConnOracle;
    private Statement stOracle;
    private ResultSet rsOracle;
    private ArrayList<String> Error;
    private String TableName;
    private Boolean Terminited;
    private Boolean Running;
    
    public ArrayList<String> getError() {
        return Error;
    }
    
    public String getTableName() {
        return TableName;
    }
    
    public Boolean getTerminited() {
        return Terminited;
    }
    
    public void setTerminited(Boolean value) {
        Terminited=value;
    }
    
    public InsThread(Connection vConnMSSQL, Connection vConnOracle, String vTableName) throws SQLException {
        ConnMSSQL=vConnMSSQL;
        ConnOracle=vConnOracle;
        Error = new ArrayList<>();
        TableName=vTableName;
        Terminited=false;
    }
    
    @Override
    public void run() {
        String FirstCol;
        Date dt = new Date();
        Running=true;
        try {
           DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
           ConnOracle = DriverManager.getConnection(
                "jdbc:oracle:thin:@"+DeloOraToMsSqlMigra.params.get("O"), 
                DeloOraToMsSqlMigra.params.get("OU"), 
                DeloOraToMsSqlMigra.params.get("OP"));
           ConnOracle.setAutoCommit(false);
            stOracle = ConnOracle.createStatement();
            rsOracle= stOracle.executeQuery("select * from " + TableName);
            String SqlInsert = "INSERT INTO " + TableName + " VALUES(";
            for (int i=1; i<=rsOracle.getMetaData().getColumnCount(); i++) {
                if (i<rsOracle.getMetaData().getColumnCount()) {
                    SqlInsert+="?,";
                } else {
                    SqlInsert+="?); ";           
                }
            }
            stMSSQL = ConnMSSQL.prepareStatement(SqlInsert);
            ConnMSSQL.setAutoCommit(false);
            while (rsOracle.next()) {
               if (Terminited) {
                   break;
               }
               FirstCol=rsOracle.getString(1);
               for (int i=1; i<=rsOracle.getMetaData().getColumnCount(); i++) {
                   try {
                    if (rsOracle.getObject(i) != null) {
                        switch (rsOracle.getMetaData().getColumnType(i)) {
                            case 2: {
                                stMSSQL.setInt(i, rsOracle.getInt(i));
                                break;
                            }
                            case 93: {
                                stMSSQL.setTimestamp(i, rsOracle.getTimestamp(i));
                                break;
                            }
                            case -4: {
                                stMSSQL.setObject(i, rsOracle.getObject(i));
                                break;
                            }
                            case -3: {
                                stMSSQL.setBlob(i, rsOracle.getBlob(i));
                                break;
                            }
                            default: {
                                stMSSQL.setString(i, rsOracle.getString(i));
                            }
                        }
                    } else {
                        stMSSQL.setNull(i, rsOracle.getMetaData().getColumnType(i));
                    }
                   } 
                   catch (SQLException ex) {
                    Error.add(ex.getMessage()+"\nЗначение первого столбика: "+FirstCol);
                   }
               }
               stMSSQL.executeUpdate();
            } 
        }
        catch (SQLException ex) {
            Error.add(ex.getMessage());
            //System.out.print(ex.getMessage()+"\n");
        }
        finally {
            try {
                ConnMSSQL.setAutoCommit(true);
                ConnMSSQL.commit();
                stMSSQL.close();
                rsOracle.close();
                stOracle.close();
                ConnOracle.close();
            }
            catch (SQLException ex) {
                Error.add(ex.getMessage());
            }
        } 
      Running=false;
      
      Long r = ((new Date()).getTime() - dt.getTime()) / 1000;
      Error.add("Время выполнения: "+r.toString()+"\n");
    }
    
    public Boolean getRunning() {
        return Running;
    }
}
